<?php
namespace Home\Controller;


class UserAddressController extends  UserController
{

    //加载收货地址列表
    function getlist()
    {
        $user_id = UID;
        if ($user_id <= 0) {
            $retData['status'] = 0;
            $retData['content'] = "登录超时，请重新登录";
            echo json_encode($retData);
            exit;
        }

        $_is_admin_mng = empty($_REQUEST['_is_admin_mng']) ? 0 : 1;

        $data_list = M('user_address')->where(array('user_id' => $user_id))->select();

        $this->assign("_is_admin_mng", $_is_admin_mng);
        $this->assign("data_list", $data_list);

        $content = $this->fetch();


        $retData['status'] = 1;
        $retData['content'] = $content;
        echo json_encode($retData);
        exit;

    }

    //添加/编辑收货地址
    function update_add()
    {

        $consignee = trim(I("consignee"));
        $mobile = trim(I("tel"));
        $province = trim(I("province"));
        $city = trim(I("city"));
        $district = trim(I("district"));
        $address = trim(I("address"));
        $zipcode = trim(I("zipcode"));
        $idcard = trim(I("idcard"));
        $card_record_id = intval(I("card_record_id"));

        //身份证正面图片base64
        $idcard_img = trim(I("idcard_img"));
        //身份证反面图片base64
        $idcard_img2 = trim(I("idcard_img2"));
        $source = trim(I("source"));

        $edit_con_id = trim(I("edit_con_id"));
        $edit_con_id = empty($edit_con_id) ? 0 : intval($edit_con_id);



        //保存日志
        write_file_aomai_ftp_v2('idcard_img', "idcard_img", $idcard_img);

        write_file_aomai_ftp_v2('idcard_img2', "idcard_img2", $idcard_img2);




        if (empty($consignee)) {
            $this->error("请输入收货人姓名");
        }

        if (empty($mobile)) {
            $this->error("请输入联系电话");
        } else {

            if (!preg_match("/1[23456789]{1}\d{9}$/", $mobile)) {
                $this->error("手机号码格式错误，请输入11位数的手机号码");
            }

        }

        if ($province == "0") {
            $this->error("请选择省份");
        }
        if ($city == "0") {
            $this->error("请选择城市");
        }
        if ($district == "0") {
            $this->error("请选择地区", 2000);
        }

        if ($address == "") {
            $this->error("请输入详细地址");
        }
        $user_id = $_SESSION['user_id'];//用户ID



        if($edit_con_id==0){

            if($source==2){


                //身份证接口收费金额
                $photo_clearance_fee = M('shop_config')->where(array('code' => 'photo_clearance_fee'))->getField('value');



                //用户金额
                $user_money = M('users')->where(array('user_id' => $user_id))->getField('user_money');

                //如果用户余额小于扣费余额则提示充值
                if ($user_money < $photo_clearance_fee) {
                    $this->error("预存款余额不足，请及时充值", 3000);
                    exit;
                }
 
                $old_card_record = M('card_record')->where(array('consignee' => $consignee, 'user_id' => $user_id , 'id_card' => $idcard ,'type' =>2))->find();

                //身份证接口收费
                if(empty($old_card_record)){
                    $res = photo_clearance_fee($photo_clearance_fee, $user_id);
                }
            }
        }


        //如果身份证记录ID不为空，则去查找记录
        if (!empty($card_record_id)) {
            $card_record_entity = M('card_record')->where(array('id' => $card_record_id))->find();
        } else {
            //如果身份证ID为空，则新增记录
            $card_record['consignee'] = $consignee;
            $card_record['id_card'] = $idcard;
            $card_record['add_time'] = time();
            $card_record['user_id'] = $user_id;
            $card_record['type'] = $source;
            $card_record_id = M('card_record')->add($card_record);
        }


        $dir_card = $_SERVER['DOCUMENT_ROOT'] . '/attas/cardsimg/cards/' . $card_record_id . '/';//检验存储身份证用户ID文件夹是否存在
        //文件夹不存在则创建
        if (!is_readable($dir_card)) {
            is_file($dir_card) or mkdir($dir_card, 0700);// 0777，意味着最大可能的访问权
        }

        $name = date('Ymd');
        $image = $user_id . '_' . $name . uniqid() . rand(1111, 9999);


        $image_name = $card_record_id . '/' . $image . ".jpg";//正面图片名字
        $image_name2 = $card_record_id . '/' . $image . "_1.jpg";//反面图片名字


        //订单表是否有身份证正面图片是现在身份证记录表这张图片
//        $order_idcard_img=M('order_info')->where(array('idcard_img'=>$card_record_entity['idcard_img']))->count();

        //订单表是否有身份证反面图片是现在身份证记录表这张图片
//        $order_idcardanti_img=M('order_info')->where(array('idcardanti_img'=>$card_record_entity['idcard_img2_name']))->count();


//        $wh['idcard_img'] =$card_record_entity['idcard_img'];
//        $wh['_string'] ='!FIND_IN_SET(id,"'.$card_record_id.'")';

        //身份证记录表是否有身份证正面图片在不是自身记录上使用的
//        $card_record_idcard_img=M('card_record')->where($wh)->count();


//        $where_cr['idcardanti_img'] =$card_record_entity['idcard_img2_name'];
//        $where_cr['_string'] ='!FIND_IN_SET(id,"'.$card_record_id.'")';

        //身份证记录表是否有身份证反面图片在不是自身记录上使用的
//        $card_record_idcardanti_img=M('card_record')->where($where_cr)->count();


        //获取身份证正面图片名字
        $idcard_img_name = trim(I('idcard_img_name'));
        //如果身份证正面图片名字和身份证记录表图片不一致，则又重新上传，则保存数据
        if ($idcard_img_name != $card_record_entity['idcard_img']) {
            //如果身份证有上传正面图片，则保存
            if (!empty($idcard_img)) {
                //得到图片的物理路径【正面】
                $idcard_img_1_path = $_SERVER['DOCUMENT_ROOT'] . '/attas/cardsimg/cards/' . $image_name;
                //保存上传的图片
                create_img_by_base64_for_tp($idcard_img, $idcard_img_1_path);

                //给图片添加水印
                aomai_makemarkweak($idcard_img_1_path);

                $save_data['idcard_img'] = $image_name;
                $save_card_record['idcard_img'] = $image_name;

                $new_idcard_file = $_SERVER['DOCUMENT_ROOT'] . '/attas/cardsimg/backups/' . time() . '.jpg';
                copy($idcard_img_1_path, $new_idcard_file);

            }
        } else {
            $save_data['idcard_img'] = $idcard_img_name;
            $save_card_record['idcard_img'] = $idcard_img_name;
        }


        //获取身份证反面图片名字
        $idcardanti_img_name = trim(I('idcard_img2_name'));


        //如果身份证反面图片名字和身份证记录表图片不一致，则又重新上传，则保存数据
        if ($idcardanti_img_name != $card_record_entity['idcardanti_img']) {


            //如果身份证有上传反面图片，则保存
            if (!empty($idcard_img2)) {

                //得到图片的物理路径【反面】
                $idcard_img_2_path = $_SERVER['DOCUMENT_ROOT'] . '/attas/cardsimg/cards/' . $image_name2;
                //保存上传的图片
                create_img_by_base64_for_tp($idcard_img2, $idcard_img_2_path);

                //给图片添加水印
                aomai_makemarkweak($idcard_img_2_path);

                $save_data['idcardanti_img'] = $image_name2;
                $save_card_record['idcardanti_img'] = $image_name2;

                $idcardanti_file = $_SERVER['DOCUMENT_ROOT'] . '/attas/cardsimg/backups/' . time() . '_1.jpg';
                copy($idcard_img_2_path, $idcardanti_file);
            }

        } else {
            $save_data['idcardanti_img'] = $idcardanti_img_name;
            $save_card_record['idcardanti_img'] = $idcardanti_img_name;
        }


        M('card_record')->where(array('id' => $card_record_id))->save($save_card_record);

        $save_data['card_record_id'] = $card_record_id;
        $save_data['source'] = $source;


        //如果是编辑
        if ($edit_con_id > 0) {

            $save_data['consignee'] = $consignee;
            $save_data['tel'] = $mobile;
            $save_data['idcard'] = $idcard;
            $save_data['province'] = $province;
            $save_data['city'] = $city;
            $save_data['district'] = $district;
            $save_data['address'] = $address;
            $save_data['zipcode'] = $zipcode;
            $save_data['country'] = 1;

            M('user_address')->where(array('address_id' => $edit_con_id, 'user_id' => UID))->save($save_data);

            $this->success("修改成功");
            exit;
        }

//        $where['consignee']=$consignee;
//        $where['mobile']=$mobile;
//        $where['idcard']=$idcard;
//        $where['user_id']=UID;
//        
//        $user_address_entity=M('user_address')->where($where)->find();
//        if($user_address_entity)
//        {
//            $this->error("该身份证号码已添加了收货地址");
//        }

        $save_data['consignee'] = $consignee;
        $save_data['tel'] = $mobile;
        $save_data['idcard'] = $idcard;
        $save_data['province'] = $province;
        $save_data['city'] = $city;
        $save_data['district'] = $district;
        $save_data['address'] = $address;
        $save_data['zipcode'] = $zipcode;
        $save_data['user_id'] = UID;
        $save_data['country'] = 1;
        $save_data['source'] = $source;

        M('user_address')->add($save_data);

        $this->success("添加成功");

    }

    //加载收货地址列表
    function get_entity()
    {
        $user_id = UID;
        if ($user_id <= 0) {
            $retData['status'] = 0;
            $retData['content'] = "登录超时，请重新登录";
            echo json_encode($retData);
            exit;
        }

        $address_id = I('id', 0);

        $data = M('user_address')->where(array('user_id' => $user_id, 'address_id' => $address_id))->find();
        if (!empty($data)) {


            $retData['status'] = 1;
            $retData['content'] = $data;
            echo json_encode($retData);
            exit;
        }
        $retData['status'] = 0;
        $retData['content'] = "记录不存在";
        echo json_encode($retData);
        exit;
    }


    //是否启用身份证接口
    public function get_card_status()
    {


        $card_status = M('shop_config')->where(array('code' => 'Interface'))->getField('value');
        $id_card_fee = M('shop_config')->where(array('code' => 'id_card_fee'))->getField('value');
        $photo_clearance_fee = M('shop_config')->where(array('code' => 'photo_clearance_fee'))->getField('value');

        $Data['card_status'] = $card_status;
        $Data['id_card_fee'] = $id_card_fee;
        $Data['photo_clearance_fee'] = $photo_clearance_fee;
        echo json_encode($Data);
        exit;

    }

    public function compose_img()
    {

        $consignee = trim(I('consignee'));//姓名

        $ID_card = trim(I('ID_card'));//身份证号码

        $user_id = intval(I('user_id'));//用户ID


        $sql = "select count(id) AS a from ki_card_record  WHERE user_id='" . $user_id . "' and type=1 and add_time BETWEEN  UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL (60*24) MINUTE)) and UNIX_TIMESTAMP(now())";

        $ret = M('')->query($sql);

        $day_card_record = empty($ret[0]) ? "" : $ret[0]['a'];


        if ($day_card_record > 100) {
            $retData['status'] = 0;
            $retData['content'] = "今天请求次数超过一百次，请明天再次请求！";
            echo json_encode($retData);
            exit;
        }

        //身份证接口收费金额
        $id_card_fee = M('shop_config')->where(array('code' => 'id_card_fee'))->getField('value');

        //去身份证记录表查找是否接口有记录
        $card_record = M('card_record')->where(array('id_card' => $ID_card, 'type' => 1))->find();

        //用户金额
        $user_money = M('users')->where(array('user_id' => $user_id))->getField('user_money');


        //如果记录不为空则直接读取
        if (!empty($card_record)) {
            //判断获取的姓名和记录姓名是否一致
            if ($consignee != $card_record['consignee']) {
                //不一致则输出错误信息
                $retData['status'] = 0;
                $retData['content'] = "身份信息不匹配请重新输入！";
                echo json_encode($retData);
                exit;
            } else {

                if ($user_id != $card_record['user_id']) {
                    //如果用户余额小于扣费余额则提示充值
                    if ($user_money < $id_card_fee) {
                        $retData['status'] = 3;
                        $retData['content'] = '您的预存款余额不足，请先充值，<br/>点击下面“确认充值” 按钮前往充值，<br/>充值后再回到该界面进行操作。';
                        echo json_encode($retData);
                        exit;
                    }
                    //身份证接口收费
                    $res = ID_card_fee($id_card_fee, $user_id);
                }

                //一致则直接把图片信息读取出来
                $retData['status'] = 1;
                $retData['card_record_id'] = $card_record['id'];
                $retData['card_1'] = $card_record['idcard_img'];
                $retData['card_2'] = $card_record['idcardanti_img'];
                echo json_encode($retData);
                exit;

            }
        } else {

            //如果用户余额小于扣费余额则提示充值
            if ($user_money < $id_card_fee) {
                $retData['status'] = 3;
                $retData['content'] = '您的预存款余额不足，请先充值，<br/>点击下面“确认充值” 按钮前往充值，<br/>充值后再回到该界面进行操作。';
                echo json_encode($retData);
                exit;
            }


            //如果记录为空则去请求API接口获取信息
            $json_string = _get_card_information($consignee, $ID_card);//传入姓名和身份证

            $card_data = json_decode($json_string);//解析获取的json数据

            $ResponseCode = $card_data->ResponseCode;//判断是否请求成功

            $Result = $card_data->Identifier->Result;//数据是否一致


            //数据请求成功
            if ($ResponseCode == 100 && $Result == '一致') {


                //身份证接口收费
                $res = ID_card_fee($id_card_fee, $user_id);

                $add_data['consignee'] = $consignee;
                $add_data['id_card'] = $ID_card;
                $add_data['add_time'] = time();
                $add_data['user_id'] = $user_id;
                $add_data['type'] = 1;
                $card_record_id = M('card_record')->add($add_data);//新增身份证记录表记录

                $_SESSION['card_record_id'] = $card_record_id;//将身份证记录ID存入session

                $md5_ID = md5($card_data->Identifier->IDNumber . '951753');//通过身份证号码和随机数生成md5


                $image = $card_data->Identifier->Photo;//身份证头像的base64

                $img_decode = base64_decode(str_replace(" ", "+", $image));//把头像的base64中空格替换为+

                $img = imagecreatefromstring($img_decode);//通过文件流创建图片


                $dir_head = $_SERVER['DOCUMENT_ROOT'] . '/attas/cardsimg/heads/' . $card_record_id . '/';//检验存储头像用户ID文件夹是否存在
                //文件夹不存在则创建
                if (!is_readable($dir_head)) {
                    is_file($dir_head) or mkdir($dir_head, 0700);// 0777，意味着最大可能的访问权
                }


                $newFilePath = './attas/cardsimg/heads/' . $card_record_id . '/' . $md5_ID . '.jpg';//头像的保存路径和图片名字


                imagepng($img, $newFilePath);//保存图片到本地
                imagedestroy($img);//销毁文件流

                $head_img=$_SERVER['DOCUMENT_ROOT'] .'/attas/cardsimg/heads/' . $card_record_id . '/' . $md5_ID . '.jpg';


                if (!file_exists($head_img)) {
                    echo json_encode(array("status" => 0, 'info' => '身份证图片头像不存在'));
                    exit;
                }


                $name = $card_data->Identifier->Name;//姓名

                $Sex_two = $card_data->Identifier->Sex;//性别

                //mb_substr函数是作为截取
                $Sex = mb_substr($Sex_two, 0, 1, 'utf-8');

                $Birthday = $card_data->Identifier->Birthday;//生日

                $year = mb_substr($Birthday, 0, 4, 'utf-8');

                $month = mb_substr($Birthday, 5, 2, 'utf-8');

                $day = mb_substr($Birthday, 8, 2, 'utf-8');

                $Address = $card_data->Identifier->Address;//住址

                $address = mb_substr($Address, 0, 11, 'utf-8');

                $IDNumber = $card_data->Identifier->IDNumber;//身份证号码


                $NativePlace = $card_data->Identifier->NativePlace;//籍贯

                $beijing_str = strpos($NativePlace, '北京市');

                $shanghai_str = strpos($NativePlace, '上海市');


                if (($beijing_str !== false) || ($shanghai_str !== false)) {

                    $city = mb_substr($NativePlace, 0, 3, 'utf-8');
                    $area = mb_substr($NativePlace, 3, 6, 'utf-8');
                    $native_place = $city . '公安局' . $area . '分局';
                } else {
                    $str_count = $this->utf_8_length($NativePlace);//得到籍贯的长度

                    /*如果字符长度大于6则截取6位*/
                    if ($str_count > 6) {
                        $city = mb_substr($NativePlace, -3, 3, 'utf-8');
                    } else {
                        $city = mb_substr($NativePlace, 3, 6, 'utf-8');
                    }

                    $native_place = $city . '公安局';
                }


                $dir_card = $_SERVER['DOCUMENT_ROOT'] . '/attas/cardsimg/cards/' . $card_record_id . '/';//检验存储身份证用户ID文件夹是否存在
                //文件夹不存在则创建
                if (!is_readable($dir_card)) {
                    is_file($dir_card) or mkdir($dir_card, 0700);// 0777，意味着最大可能的访问权
                }


                $image = new \Think\Image();//调用think php添加水印函数

                /*
                 * open打开原图片
                 * text为文字水印
                 * water为图片水印
                 * save打好水印后保存的图片
                 * crop裁剪图片
                 * thumb_img 压缩图片
                 * */
                $image->open('./images/card.jpg')->text($name, './data/font/simhei.ttf', 14, '#000105', array(124, 59))->save('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '.jpg');

                $image->open('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '.jpg')->text($Sex, './data/font/simhei.ttf', 14, '#000105', array(124, 104))->save('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '.jpg');

                $image->open('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '.jpg')->text($year, './data/font/simhei.ttf', 14, '#000105', array(124, 149))->save('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '.jpg');

                $image->open('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '.jpg')->text($month, './data/font/simhei.ttf', 14, '#000105', array(214, 149))->save('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '.jpg');

                $image->open('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '.jpg')->text($day, './data/font/simhei.ttf', 14, '#000105', array(269, 149))->save('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '.jpg');

                $image->open('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '.jpg')->text($address, './data/font/simhei.ttf', 14, '#000105', array(124, 191))->save('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '.jpg');

                $image->open('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '.jpg')->text($IDNumber, './data/font/OCR-B_10_BT.ttf', 20, '#000000', array(212, 321))->save('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '.jpg');

                $image->open('./attas/cardsimg/heads/' . $card_record_id . '/' . $md5_ID . '.jpg')->thumb(178, 220, \Think\Image::IMAGE_THUMB_CENTER)->save('./attas/cardsimg/heads/' . $card_record_id . '/' . $md5_ID . '.jpg');

                $image->open('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '.jpg')->water('./attas/cardsimg/heads/' . $card_record_id . '/' . $md5_ID . '.jpg', array(370, 50))->save('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '.jpg');

                $image->open('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '.jpg')->text($native_place, './data/font/simhei.ttf', 14, '#000105', array(244, 641))->save('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '_big.jpg');

                $image->open('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '_big.jpg')->crop(599, 375)->save('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '_api.jpg');//API身份证正面原本

                $image->open('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '_big.jpg')->crop(599, 380, 0, 375)->save('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '_api_1.jpg');//API身份证反面原本

                $image->open('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '_big.jpg')->crop(599, 375)->save('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '.jpg');

                $image->open('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '_big.jpg')->crop(599, 380, 0, 375)->save('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '_1.jpg');

                $this->thumb_img('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '.jpg', 599, './attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '.jpg');

                $this->thumb_img('./attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '_1.jpg', 599, './attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '_1.jpg');


                $file = './attas/cardsimg/cards/' . $card_record_id . '/' . $md5_ID . '_big.jpg';//身份证大图路径

                $card_status = file_exists($file);  //检验身份证大图是否存在

                @unlink($file);//删除身份证大图

                if ($card_status == true) {
                    $card_1 = $card_record_id . '/' . $md5_ID . '.jpg';
                    $card_2 = $card_record_id . '/' . $md5_ID . '_1.jpg';

                    $save_data['idcard_img'] = $card_1;
                    $save_data['idcardanti_img'] = $card_2;

                    M('card_record')->where(array('id' => $card_record_id))->save($save_data);

                    $retData['status'] = 1;
                    $retData['card_record_id'] = $card_record_id;
                    $retData['card_1'] = $card_1;
                    $retData['card_2'] = $card_2;
                    echo json_encode($retData);
                    exit;
                } else {
                    $retData['status'] = 0;
                    $retData['content'] = "记录不存在";
                    echo json_encode($retData);
                    exit;
                }

            } elseif ($Result == '不一致') {
                $retData['status'] = 0;
                $retData['content'] = "身份证号和姓名不匹配";
                echo json_encode($retData);
                exit;
            } else {
                $retData['status'] = 0;
                $retData['content'] = "身份证号码错误";
                echo json_encode($retData);
                exit;
            }


        }


    }

    public function test()
    {

        $NativePlace = '重庆市南岸区';

        $beijing_str = strpos($NativePlace, '北京市');

        $shanghai_str = strpos($NativePlace, '上海市');


        if (($beijing_str !== false) || ($shanghai_str !== false)) {

            $city = mb_substr($NativePlace, 0, 3, 'utf-8');
            $area = mb_substr($NativePlace, 3, 6, 'utf-8');
            $native_place = $city . '公安局' . $area . '分局';
        } else {
            $str_count = $this->utf_8_length($NativePlace);//得到籍贯的长度

            /*如果字符长度大于6则截取6位*/
            if ($str_count > 6) {
                $city = mb_substr($NativePlace, -3, 3, 'utf-8');
            } else {
                $city = mb_substr($NativePlace, 3, 6, 'utf-8');
            }

            $native_place = $city . '公安局';
        }

    }


    function utf_8_length($str)
    {
        if (empty($str)) {
            return 0;
        }
        if (function_exists('mb_strlen')) {
            return mb_strlen($str, 'utf-8');
        } else {
            preg_match_all("/./u", $str, $ar);
            return count($ar[0]);
        }
    }

    /*
    * $img_path 被压缩的图片的路径
    * $thumb_w 压缩的宽
    * $save_path 压缩后图片的存储路径
    */
    public function thumb_img($img_path, $thumb_w, $save_path)
    {
        $image = new \Think\Image();
        $image->open($img_path);
        $width = $image->width(); // 返回图片的宽度
        if ($width > $thumb_w) {
            $width = $width / $thumb_w; //取得图片的长宽比
            $height = $image->height();
            $thumb_h = ceil($height / $width);
        }
        $image->thumb($thumb_w, $thumb_h)->save($save_path);
    }


    public function update_card_img()
    {


        $card_entity = M('card_record')->where(array('id' => 147))->find();
//        foreach ($card_record_list as $key=>$value){
        $data['idcard_img'] = $card_entity['idcard_img'];
        $data['idcardanti_img'] = $card_entity['idcardanti_img'];
        $data['id_card'] = $card_entity['id_card'];

        $where['idcard'] = $card_entity['id_card'];
//            $where['add_time']=array('GT',1473652800);
//            $where['consignee']=$value['consignee'];
        M('order_info')->where($where)->save($data);
//        }


        echo '操作成功';
        exit;


    }


    public function update_user_address()
    {

        $card_api_select = M('card_record')->where(array('type' => 1))->field('id,idcard_img,idcardanti_img,id_card')->select();


        foreach ($card_api_select as $key => $value) {


            $address_data['idcard_img'] = $value['idcard_img'];
            $address_data['idcardanti_img'] = $value['idcardanti_img'];
            $address_data['source'] = 1;

            $ad_where['idcard'] = $value['id_card'];

            M('user_address')->where($ad_where)->save($address_data);

            $order_data['idcard_img'] = $value['idcard_img'];
            $order_data['idcardanti_img'] = $value['idcardanti_img'];


            $where['idcard'] = $value['id_card'];
            $where['order_id'] = array('GT', 25401);

            M('order_info')->where($where)->save($order_data);

            write_file_aomai_ftp_v2('userAddress', "userAddress", $value['id']);

        }

        echo '操作成功';
        exit;


    }



}








